package com.xinghen.ace.auth.controller;

import com.xinghen.ace.auth.configuration.KeyConfiguration;
import com.xinghen.ace.auth.service.AuthClientService;
import com.xinghen.ace.common.response.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xinghen
 * @ClassName: ClientController
 * @Package: com.xinghen.ace.auth.controller
 * @Description client controller
 * @create 2018/5/4 16:08
 */
@RestController
@RequestMapping("client")
public class ClientController {

    @Autowired
    private AuthClientService authClientService;
    @Autowired
    private KeyConfiguration keyConfiguration;

    @RequestMapping(value = "/token", method = RequestMethod.POST)
    public RestResult<String> getAccessToken(String clientId, String secret) throws Exception {
        return RestResult.ok(authClientService.apply(clientId, secret));
    }

    @RequestMapping(value = "/myClient")
    public RestResult<List<String>> getAllowedClient(String serviceId, String secret) {
        return RestResult.ok(authClientService.getAllowedClient(serviceId, secret));
    }

    @RequestMapping(value = "/servicePubKey", method = RequestMethod.POST)
    public RestResult<byte[]> getServicePublicKey(@RequestParam("clientId") String clientId, @RequestParam("secret") String secret) throws Exception {
        authClientService.validate(clientId, secret);
        return RestResult.ok(keyConfiguration.getServicePubKey());
    }

    @RequestMapping(value = "/userPubKey", method = RequestMethod.POST)
    public RestResult<byte[]> getUserPublicKey(@RequestParam("clientId") String clientId, @RequestParam("secret") String secret) throws Exception {
        authClientService.validate(clientId, secret);
        return RestResult.ok(keyConfiguration.getUserPubKey());
    }


}
